#![allow(dead_code)]

pub fn binary_search(arr: Vec<i32>, target: i32) -> Option<usize> {
    let (mut l, mut r) = (0usize, arr.len());
    while l < r {
        let mid = l + (r - l) / 2;
        let mid_val = arr[mid];
        match target.cmp(&mid_val) {
            std::cmp::Ordering::Less => r = mid,
            std::cmp::Ordering::Equal => return Some(mid),
            std::cmp::Ordering::Greater => l = mid + 1,
        }
    }
    None
}

#[test]
fn test_binary_search() {
    assert_eq!(Some(4), binary_search(vec![1, 2, 3, 4, 5, 6, 7, 8, 9], 5));
}
